/*cd C:\商业项目\Stata\4.17多重DID
import excel thesis.xlsx,first clear
rename city city
gen treat=0
replace treat=1 if city=="上海"|city=="天津"|city=="福建"|city=="广东"|city=="辽宁"|city=="浙江"|city=="河南"|city=="湖北"|city=="重庆"|city=="四川"|city=="陕西"|city=="海南"
*/

cd C:\商业项目\Stata\4.17多重DID
import excel 2.xlsx,first clear
gen time=0
replace time=1 if YEAR-starttime>=0&treat==1
//winsor2 TL time infre lngov trade ser indus ,replace cuts(1 99)/*缩尾*/
gen event = YEAR- starttime if treat==1
tab event, gen(eventt)
forvalues i = 1/17{
 replace eventt`i' = 0 if eventt`i' == .
}
drop eventt1
egen code=group(AREA)
xtset code YEAR
//reg TL time infre lngov trade ser indus eventt* ,r
xtreg TS time  infre lngov trade ser indus eventt* i.YEAR ,r
coefplot, ///
   keep(eventt*)  ///
   coeflabels(eventt1 = "-10"   ///
   eventt2 = "-9"              ///
   eventt3 = "-8"              ///
   eventt4 = "-7"               ///
   eventt5  = "-6"              ///
   eventt6  = "-5"              ///
   eventt7  = "-4"              ///
   eventt8  = "-3"              ///
   eventt9 = "-2"             ///
   eventt10 = "-1"               ///
   eventt11  = "0"              ///
   eventt12 = "1"              ///
   eventt13 = "2"              ///
   eventt14 = "3"              ///
   eventt15 = "4"              ///
   eventt16 = "5"               ///
   eventt17 = "6"  )            ///
   vertical                    ///
   yline(0)                    ///
   ytitle("Coef")              ///
   xtitle("Time passage relative to year of adoption of implied contract exception") ///
   addplot(line @b @at)        ///
   ciopts(recast(rcap))       
           ///
//coefplot,keep( eventt*) vertical recast(connect) yline(0)


save 3.dta,replace

use 3.dta,clear
//描述性统计
winsor2 TS TL time infre lngov trade ser indus ,replace cuts(1 99)/*缩尾*/
outreg2 using ts描述性, word replace sum(log) sortvar(TS TL time infre lngov trade ser indus) keep(TS TL time infre lngov trade ser indus)

//相关性
logout,save(相关性) word replace:pwcorr_a TS TL time infre lngov trade ser indus, star1(0.01) star5(0.05) star10(0.1)

//主回归
use 3.dta,clear
winsor2 TS time infre lngov trade ser indus ,replace cuts(1 99)/*缩尾*/
reg TS time  infre lngov trade ser indus 

est sto r1
outreg2 [r1 ] using TS主回归,word replace tstat bdec(4) tdec(2) sortvar(TS time infre lngov trade ser indus) keep(TS time infre lngov trade ser indus)


//安慰剂
use 3.dta,clear

gen fta_b=0
replace fta_b=1 if YEAR-starttime>=-4&starttime!=.
xtreg TS fta_b infre lngov trade ser indus i.YEAR,fe r
est sto r1
outreg2 [r1 ] using TS安慰剂,word replace tstat bdec(4) tdec(2) sortvar(TS fta_b infre lngov trade ser indus) keep(TS fta_b infre lngov trade ser indus)

